package com.onluy.sysman.controller.sysmgr;

import com.alibaba.fastjson.JSON;
import com.onluy.aliyunapi.sms.doman.SMS;
import com.onluy.aliyunapi.sms.entity.SendSmsResp;
import com.onluy.frameworkcloud.Param.ReqHead;
import com.onluy.frameworkcloud.Param.ReqInfo;
import com.onluy.frameworkcloud.Param.RspInfo;
import com.onluy.frameworkcloud.base.AppWalkController;
import com.onluy.frameworkcloud.data.util.InParam;
import com.onluy.frameworkcloud.tools.utils.AccountValidatorUtil;
import com.onluy.sysman.param.RandomCodeParam;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@Validated
@RestController
@RequestMapping("/sms")
@Api(value="SMS", description="/短信管理接口", position=9999 )
public class SmsController extends AppWalkController {



    @Resource(name = "aliSMS")
    private SMS aliSMS;

    @RequestMapping(value={ "/send_random_code"},method={RequestMethod.POST })
    @ApiOperation(value="发送随机码短信", notes="发送随机码短信")
    public RspInfo<InParam> doSendRandomCode(@RequestBody @Valid ReqInfo<ReqHead, RandomCodeParam> reqInfo )  throws Exception{
        log.info(" send_random_code reqInfo :"+ JSON.toJSONString(reqInfo));
        RspInfo<InParam> jsonResult = new RspInfo<InParam>();
        RandomCodeParam randomCodeParam=reqInfo.getReqBody();
        int num = 0;
        String errstr = null;
//        if( redisUtil.hasKey("randomcode"+sysUser.getWebName().trim())) {
//            errstr ="验证码已经发送，请在1分钟以后获取！";
//        }

        if (AccountValidatorUtil.isMobile(randomCodeParam.getPhone()) == false) {
            errstr = "该工号无有效手机号码！";
        }

        if(StringUtil.isNullOrEmpty(errstr)==false)throw  new Exception(errstr);

        SendSmsResp response = aliSMS.SendRandCodeSms(randomCodeParam.getPhone(), randomCodeParam.getRandCode());
        if ("OK".equals(response.getCode())) {
           // redisUtil.set("randomcode" + sysUser.getWebName().trim(), code, 90);
            jsonResult.setRspCode(0);
            jsonResult.setRspDesc(response.getBizId());
        } else {
            jsonResult.setRspCode(0);
            errstr = response.getMessage();
        }

        return jsonResult;
    }

}
